﻿using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using SharppiFlowEditor.models;
using SharppiFlowEditor.views;

namespace SharppiFlowEditor.controllers
{
    /*
 * The digram controller for creating new connectors.
 */
    public class CreateConnectorController : AbstractController
    {

        // the first connected node
        private AbstractNode src = null;
        // current end point of the connector
        private float dx, dy;
        // the pen for drawing temporary lines
        private Pen tmpPen = new Pen(Color.FromArgb(150, Color.Black));
        private Point srcCenter;
        private double zoomValue;

        public CreateConnectorController(AppView appView, AppModel appModel)
        {
            Model = appModel;
            View = appView;
        }

        /*
         * Draw a temporary line while connecting.
         */
        override public void DrawTempImage(Graphics g)
        {

            zoomValue = View.document.ZoomSize;

            if (src != null)
            {
                srcCenter = new Point(src.getBoundingRect().X + src.getBoundingRect().Width / 2,
                                           src.getBoundingRect().Y + src.getBoundingRect().Height / 2);
                tmpPen.EndCap = LineCap.ArrowAnchor;
                g.DrawLine(tmpPen, (int)(srcCenter.X * zoomValue), (int)(srcCenter.Y * zoomValue), dx, dy);
            }
        }

        /*
         * Start to create the connector.
         */
        override public void ViewMouseDown(object sender, MouseEventArgs e)
        {
            scrollOffset = View.document.ViewScrollPosition;
            int x = e.X - scrollOffset.X;
            int y = e.Y - scrollOffset.Y;

            // get the first node
            src = View.document.GetNode(x, y);
            if (src != null)
            {
                dx = x;
                dy = y;
                // update the view to draw the temporary line
                View.document.Invalidate();
                View.document.Update();
            }
        }

        /*
         * Continue drawing the temporary line.
         */
        override public void ViewMouseMove(object sender, MouseEventArgs e)
        {
            scrollOffset = View.document.ViewScrollPosition;

            if (src != null)
            {
                dx = e.X - scrollOffset.X; dy = e.Y - scrollOffset.Y;
                View.document.Invalidate();
                View.document.Update();
            }
        }

        /*
         * Stop drawing the temporary line and possibly create a connector.
         */
        override public void ViewMouseUp(object sender, MouseEventArgs e)
        {
            scrollOffset = View.document.ViewScrollPosition;

            if (this.src != null)
            {
                AbstractNode src = this.src;
                this.src = null;
                // get the second connected node
                AbstractNode dest = View.document.GetNode((e.X - scrollOffset.X), (e.Y - scrollOffset.Y));
                // if there are two nodes anmd they are not 
                // already connected
                if (dest != null && !Model.Connected(View.document.GraphDocument, src, dest) && src != dest)
                {
                    Model.CreateConnector(View.document.GraphDocument, src, dest, "default");
                }
                else
                {
                    View.document.Invalidate();
                    View.document.Update();
                }
            }
        }
    }
}
